<?php

/**
 * @Author: king
 * @Date:   2019-06-05 21:42:59
 * @Last Modified by:   king
 * @Last Modified time: 2020-08-07 20:46:26
 */

namespace App\Http\Controllers\Api\v3;

use App\Http\Controllers\Api\BaseController;
use App\Models\Feedback;
use App\Tools\Astrolog\Astrolog;
use Illuminate\Http\Request;

/**
 * 星盘
 * Class Astrolabe
 * @package use App\Tools\Astrolog\Astrolog
 */
class FeedbackController extends BaseController
{

 public function create(Request $request)
{
    // 手动构建需要的字段数组
    $data = [
        'user_id' => $request->uid,
        'type' => $request->input('type'),
        'content' => $request->input('content'),
        'img' => $request->input('img'),
        'question_id' => $request->input('question_id') ??'0',
        'answer_id' => $request->input('answer_id') ?? '0',
        // Add other allowed fields as needed
    ];

    // 手动验证请求数据
    $validator = \Validator::make($data, [
        'user_id' => 'required|integer',
        'type' => ['required', 'integer'],
        'content' => 'required|string',
        'img' => 'nullable|string',
        'question_id' => 'nullable|integer',
        'answer_id' => 'nullable|integer',
        // Add other validation rules as needed
    ]);

    // 验证失败时返回错误信息
    if ($validator->fails()) {
        return show(config('code.error'), '提交意见反馈失败');
    }

    // 创建反馈
    $feedback = Feedback::create($data);

    return show(config('code.success'), '提交成功', $feedback);
}

    public function List()
    {
        $data = Feedback::where('user_id',$request->uid)->get();

        return show(config('code.success'), '加载成功', $data);
    }
    public function detail(Request $request)
    {
        $id = $request->input('id');
        $data = Feedback::where(['user_id'=>$request->uid,'id'=>$id])->first();
        return show(config('code.success'), '加载成功', $data);
    }
    public function delete($id)
    {
        $feedback = Feedback::where('user_id',$request->uid)->find($id);

        if (!$feedback) {
           return show(config('code.error'), '删除失败');
        }

        $feedback->delete();

        return show(config('code.success'), '删除成功');
    }

}
